<?php
/**
 * Created by 攻略类.
 * User: 尚金殿
 * Date: 2018/8/25
 * Time: 14:44
 */

namespace app\admin\controller;

use think\Db;

class NewsController extends CommonController
{
    protected $mysql ='news';
    protected $pk = 'id';
    protected $name = 'title';
    protected $status = 'status';

    //攻略列表
    public function lists(){
        $keyWord = $this->request->param('key_word');
        $classId = $this->request->param('classId') ? $this->request->param('classId') : 0;

        //关键词
        if(!empty($keyWord)){
            $res = Db::name($this->mysql)->where($this->name,'like',$keyWord.'%')->where($this->status,'>',-1)->paginate(10);
        }else if(!empty($classId)){
            //分类
            $res = Db::name($this->mysql)->where('tid',$classId)->where($this->status,'>',-1)->paginate(10);
        }else{
            $res = Db::name($this->mysql)->where($this->status,'>',-1)->paginate(10);
        }

        //获取全部分类
        $type = Db::name('news_type')->where('status',1)->select();

        //变量输出
        $this->assign(['key_word'=>$keyWord,'type'=>$type,'res'=>$res,'classId'=>$classId]);

        //加载模版
        return $this->fetch();
    }

    //添加新闻
    public function add(){
        if($this->request->isPost()){
            //获取提交的数据
            $data = $this->request->param();
            //判定数据
            if(!$data['tid']){
                return show(0,'新闻分类不能为空');
            }
            if(!$data['title']){
                return show(0,'新闻标题不能为空');
            }

            //判定有么有ID
            if(!empty($data['id'])){
                $this->save($data);
            }
            $data['create_time'] = time();
            //入库
            $strategyId = Db::name('news')->insertGetId($data);

            if(!$strategyId){
                return show(0,'新增失败');
            }
                return show(1,'新增成功');
        }else{
            //获取全部攻略分类
            $class = Db::name('news_type')->where('status',1)->select();

            //变量输出
            $this->assign('class',$class);

            //加载模版
            return $this->fetch();
        }
    }

    //编辑攻略
    public function edit(){
        $id = $this->request->param('id');
        //获取分类数据
        $class = Db::name('news_type')->where('status',1)->select();

        //获取攻略数据
        $news = Db::name($this->mysql)->where($this->pk,$id)->find();

        //变量输出
        $this->assign(['class'=>$class,'news'=>$news]);

        //加载模版
        return $this->fetch();
    }

    //保存编辑
    public function save($data){
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }

        $id = $data['id'];
        unset($data['id']);

        //入库
        $res = Db::name('news')->where($this->pk,$id)->update($data);

        if(!$res){
            return show(0,'更新失败');
        }
            return show(1,'更新成功');
    }
    //删除数据(修改状态)
    public function setNewsStatus(){
        $id = $this->request->param('id');
        $data['status'] = -1;

        //入库
        $res = Db::name('news')->where('id',$id)->update($data);

        if(!$res){
            return show(0,'操作失败');
        }
        return show(1,'操作成功');
    }

    //更新排序
    public function updateNewsSort(){
        //获取数据
        $sort = $this->request->param();

        //定义跳转地址
        $jumpUrl = $_SERVER['HTTP_REFERER'];

        //定义一个空数组准备接收更新排序失败的ID
        $errors = array();

        //判定是否有值
        if($sort || is_array($sort)){

            //循环进行更新数据
            foreach($sort['sort'] as $scrapId => $v){
                $data['create_time'] = time();
                $data['sort'] = $v;
                $id = Db::name($this->mysql)->where($this->pk,$scrapId)->update($data);
                if($id === false){
                    $errors[] = $scrapId;
                }
            }
            if($errors){
                return show(0,'排序失败-'.import(',',$errors),array('jump_url' => $jumpUrl));
            }
            return show(1,'排序成功',array('jump_url' => $jumpUrl));
        }

        return show(0,'没有提交的数据');
    }
    /**************************************攻略分类****************************************/

    //攻略分类列表
    public function classLists(){
        $keyWord = $this->request->param('key_word');

        //关键词
        if(!empty($keyWord)){
            $res = Db::name('news_type')->where('name','like',$keyWord.'%')->where($this->status,'>',-1)->paginate(10);
        }else{
            $res = Db::name('news_type')->where($this->status,'>',-1)->paginate(10);
        }
        $this->assign('key_word',$keyWord);
        $this->assign('res',$res);
        return $this->fetch();
    }

    //添加攻略分类
    public function classAdd(){
        if($this->request->isPost()){
            $data = $this->request->param();
            //判定数据
            if(!$data['name']){
                return show(0,'分类名称不能为空');
            }

            if(!empty($data['id'])){
                $this->classSave($data);
            }
            //入库
            $res = Db::name('news_type')->insertGetId($data);

            if(!$res){
                return show(0,'新增失败');
            }
                return show(1,'新增成功');
        }else{
            return $this->fetch();
        }
    }

    //攻略分类编辑
    public function classEdit(){
        $id = $this->request->param('id');

        //获取数据
        $class = Db::name('news_type')->where('id',$id)->find();

        //变量输出
        $this->assign('class',$class);

        //加载模版
        return $this->fetch();
    }

    //保存分类数据
    public function classSave($data){
        if(!$data || !is_array($data)){
            return show(0,'传递的数据不合法');
        }
        $id = $data['id'];
        unset($data['id']);

        //入库
        $res = Db::name('news_type')->where('id',$id)->update($data);

        if(!$res){
            return show(0,'更新失败');
        }
            return show(1,'更新成功');
    }

    //删除攻略分类
    public function classSetStatus(){
        $id = $this->request->param('id');
        $data['status'] = -1;

        //入库
        $res = Db::name('news_type')->where('id',$id)->update($data);

        if(!$res){
            return show(0,'操作失败');
        }
            return show(1,'操作成功');

    }

}